package 复习.面试TOP101.栈堆队列;

import java.util.Stack;

public class 包含min函数的栈 {

    Stack<Integer> stack = new Stack<>();//
    Stack<Integer> min = new Stack<>();

    public void push(int node) {
        if(min.isEmpty() || node < min.peek()){
            min.push(node);
        }else{
            min.push(min.peek());
        }
        stack.push(node);
    }

    public void pop() {
        if(stack != null){
            stack.pop();
            min.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int min() {
        return min.peek();
    }
}

/*
public class Solution {


    Stack<Integer> stack1 = new Stack<>();
    Stack<Integer> stack2 = new Stack<>();

    public void push(int node) {

        stack1.push(node);
        //注意这里的判断为空要放在前面
        if (stack2.isEmpty() || stack2.peek() > node) {
            stack2.push(node);
        } else {
            stack2.push(stack2.peek());
        }
    }

    public void pop() {
        stack1.pop();
        stack2.pop();
    }

    public int top() {
        if (stack1.isEmpty()) {
            throw new EmptyStackException();
        }
        return stack1.peek();
    }

    public int min() {
        return stack2.peek();
    }

}
 */